<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class dosen extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_dosen');
        // load library
        $this->load->library('encrypt');
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/dosen/index.html");
        // data session 
        $search = $this->tsession->userdata('search_dosen');
        $this->smarty->assign('search', $search);
        // params
        $nama_lengkap = !empty($search['nama_lengkap']) ? "%" . $search['nama_lengkap'] . "%" : "%";
        //--
        $params = array($nama_lengkap);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/dosen/index/");
        $config['total_rows'] = $this->m_dosen->get_total_data_dosen($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($nama_lengkap, ($start - 1), $config['per_page']);
        $result = $this->m_dosen->get_data_dosen($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search
    public function search_process() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        if ($this->input->post('save') == 'Cari') {
            // params
            $nama_lengkap = $this->input->post('nam_lengkap');
            //--
            $params = array("nama_lengkap" => $nama_lengkap);
            // set session
            $this->tsession->set_userdata('search_dosen', $params);
        } else {
            // unset session
            $this->tsession->unset_userdata('search_dosen');
        }
        // redirect
        redirect('master/dosen');
    }

    // halaman tambah data
    public function add() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/dosen/add.html");
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah data
    public function add_process() {
        // set page rules
        $this->_set_page_rule("R");
        // cek input
        $this->tnotification->set_rules('nama_lengkap', 'Nama Dosen', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('dosen_gelar', 'Gelar Dosen', 'trim|required|maxlength[20]');
        $this->tnotification->set_rules('dosen_nip', 'NIP Dosen', 'trim|required|maxlength[20]');
        $this->tnotification->set_rules('user_name', 'Username', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('user_pass', 'Password', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('dosen_status', 'Status', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $nama_lengkap = $this->input->post('nama_lengkap');
            $dosen_gelar = $this->input->post('dosen_gelar');
            $dosen_nip = $this->input->post('dosen_nip');
            $user_name = $this->input->post('user_name');
            $user_pass = $this->input->post('user_pass');
            $dosen_status = $this->input->post('dosen_status');
            //--
            // password
            $pass_key = crc32($user_pass);
            $pass = $this->encrypt->encode($user_pass, $pass_key);
            // insert akun
            $param_akun = array(
                $user_name, $pass, $pass_key, $this->com_user['user_id']
            );
            // input data
            if ($this->m_dosen->insert_akun_dosen($param_akun)) {
                $user_id = $this->m_dosen->get_last_inserted_id();
                $params = array(
                    $user_id, strtoupper($nama_lengkap),
                    $dosen_gelar, $dosen_nip,
                    $dosen_status, $this->com_user['user_id']
                );
                // insert data dosen
                $this->m_dosen->insert_data_dosen($params);
                // insert role
                $this->m_dosen->insert_role_dosen(array(4, $user_id));
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('master/dosen/add');
    }

    // halaman edit
    public function edit($user_id = "") {
        // set page rules
        $this->_set_page_rule("U");
        // set template content
        $this->smarty->assign("template_content", "master/dosen/edit.html");
        // data
        $result = $this->m_dosen->get_detail_data_dosen($user_id);
        if (!empty($result)) {
            $result['user_pass'] = $this->encrypt->decode($result['user_pass'], $result['user_key']);
            $this->smarty->assign("result", $result);
        }
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // edit process
    public function edit_process() {
        // set page rules
        $this->_set_page_rule("U");
        // cek input
        $this->tnotification->set_rules('nama_lengkap', 'Nama Dosen', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('dosen_gelar', 'Gelar Dosen', 'trim|required|maxlength[20]');
        $this->tnotification->set_rules('dosen_nip', 'NIP Dosen', 'trim|required|maxlength[20]');
        $this->tnotification->set_rules('user_name', 'Username', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('user_pass', 'Password', 'trim|required|maxlength[50]');
        $this->tnotification->set_rules('dosen_status', 'Status', 'required');
        $this->tnotification->set_rules('user_id', 'User ID', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $nama_lengkap = $this->input->post('nama_lengkap');
            $dosen_gelar = $this->input->post('dosen_gelar');
            $dosen_nip = $this->input->post('dosen_nip');
            $user_name = $this->input->post('user_name');
            $user_pass = $this->input->post('user_pass');
            $dosen_status = $this->input->post('dosen_status');
            $user_id = $this->input->post('user_id');
            //--
            // password
            $pass_key = crc32($user_pass);
            $pass = $this->encrypt->encode($user_pass, $pass_key);
            // param akun
            $param_akun = array(
                $user_name, $pass, $pass_key, $this->com_user['user_id'], $user_id
            );
            // update data
            if ($this->m_dosen->update_akun_dosen($param_akun)) {
                $params = array(
                    strtoupper($nama_lengkap),
                    $dosen_gelar, $dosen_nip,
                    $dosen_status, $this->com_user['user_id'],
                    $user_id
                );
                // insert data dosen
                $this->m_dosen->update_data_dosen($params);
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect('master/dosen/edit/' . $this->input->post('user_id'));
    }

    // halaman hapus data
    public function delete($user_id = "") {
        // set page rules
        $this->_set_page_rule("D");
        // set template content
        $this->smarty->assign("template_content", "master/dosen/delete.html");
        // data
        $result = $this->m_dosen->get_detail_data_dosen($user_id);
        if (!empty($result)) {
            $result['user_pass'] = $this->encrypt->decode($result['user_pass'], $result['user_key']);
            $this->smarty->assign("result", $result);
        }
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses delete
    public function delete_process() {
        // set page rules
        $this->_set_page_rule("D");
        // cek input
        $this->tnotification->set_rules('user_id', 'User ID', 'required');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $user_id = $this->input->post('user_id');
            // delete data
            if ($this->m_dosen->delete_akun_dosen($user_id)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                // redirect
                redirect('master/dosen');
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        // default redirect
        redirect('master/dosen/delete/' . $this->input->post('user_id'));
    }

}